package com.example.actearly_revised;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.TabActivity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


//@SuppressWarnings("deprecation")
public class TabHomePage extends TabActivity {

	private TabHost mTabHost;
	private HashMap mapTabInfo = new HashMap();
	public TabInfo mLastTab = null;
	public SQLogic entry;
	Typeface customFont;

	Intent p_logic;
	Intent intent;
	public static int profile_number;
	TabHost th;
	String [] spinnerItems= new String[]{
			"Milestone Tracker",
			"Quick List",
			"Add/Edit Child Profile",
			"Select different Child Profile"

	};
	public static int currentmilestone;

	private class TabInfo {
		private String tag;
		private Class clss;
		private Bundle args;
		private Fragment fragment;
		TabInfo(String tag, Class clazz, Bundle args) {
			this.tag = tag;
			this.clss = clazz;
			this.args = args;

		}
	}

	class TabFactory implements TabContentFactory {

		private final Context mContext;

		/**
		 * @param context
		 */
		public TabFactory(Context context) {
			mContext = context;
		}

		public View createTabContent(String tag) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}

	}

	@SuppressLint({ "NewApi", "NewApi" })
	@Override
	public void onCreate(Bundle savedInstanceState) {

		customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");



		p_logic = getIntent();
		profile_number = p_logic.getIntExtra("profile_number",1);

		SQLogic entry = new SQLogic(TabHomePage.this);
		entry.open();
		User currUser= entry.getUserData(profile_number);
		entry.close();

		//make action bar font white and add custom font
		getActionBar().setTitle(currUser.getChildname()+"'s milestones");


		super.onCreate(savedInstanceState);
		th = getTabHost();
		Intent i;
		i = new Intent().setClass(this, MilestoneTracker.class);
		i.putExtra("profile_number", profile_number);



		// need to make this text consistent with size/font of the app
		TextView tabText = new TextView(this);
		tabText.setText("Milestone Tracker");
		tabText.setPadding(8,9,8,9);
		tabText.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tabText.setTypeface(customFont);

		//this sets the text in the tab according to the specs defined above
		TabSpec sp = th.newTabSpec("Tab1");
		sp.setIndicator(tabText);
		sp.setContent(i);
		th.addTab(sp);


		//  Intent i;
		i = new Intent().setClass(this, NotesPage.class);
		i.putExtra("profile_number", profile_number);
		TextView tabText2 = new TextView(this);
		tabText2.setText("Notes");
		tabText2.setPadding(8,9,8,9);
		tabText2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tabText2.setTypeface(customFont);
		sp = th.newTabSpec("Tab2");
		sp.setIndicator(tabText2);
		sp.setContent(i);
		th.addTab(sp);

		i = new Intent().setClass(this, SummaryPage.class);
		i.putExtra("profile_number", profile_number);
		TextView tabText3 = new TextView(this);
		tabText3.setText("Tracker Results");
		tabText3.setPadding(8,9,8,9);
		tabText3.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tabText3.setTypeface(customFont);
		sp = th.newTabSpec("Tab3");
		sp.setIndicator(tabText3);
		sp.setContent(i);
		th.addTab(sp);

		th.setOnTabChangedListener(new OnTabChangeListener() {

			public void onTabChanged(String arg0) {
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(th.getApplicationWindowToken(), 0);

				SQLogic entry = new SQLogic(TabHomePage.this);
				entry.open();
				User currUser= entry.getUserData(profile_number);
				entry.close();
				getActionBar().setTitle(currUser.getChildname());

				if (th.getCurrentTab()==0)
				{
					getActionBar().setTitle(currUser.getChildname()+"'s milestones");
				}
				else if (th.getCurrentTab()==1)
				{
					getActionBar().setTitle(currUser.getChildname()+"'s notes");

				}
				else if (th.getCurrentTab()==2)
				{

					getActionBar().setTitle(currUser.getChildname()+"'s summary");
				}
				setTabColor(th);
				//personalize action bar
			}
		});
		setTabColor(th);
	}

	//Change The Background Color of Tabs
	public void setTabColor(TabHost tabhost) {

		for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
			tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Integer.parseInt("FFFFFF", 16)+0xFF000000); //unselected

		if(tabhost.getCurrentTab()==0)
			tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Integer.parseInt("FFEDDB", 16)+0xFF000000); //1st tab selected

		else
			tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Integer.parseInt("FFEDDB", 16)+0xFF000000); //2nd tab selected
	}


	@Override
	//public boolean onOptionsItemSelected(MenuItem item) {
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.actionbar_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/*menu is created in actionbar_menu.xml. the order each item is defined in that xml file is
	 * the order that the items appear in the menu as well
	 * this code defines what happens when each item in the menu is clicked
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.editprofilepage)
		{			
			intent = new Intent(TabHomePage.this, PickProfile.class);
			intent.putExtra("NoUsers", false);
			intent.putExtra("ModeSelected", "AddEditProfile");
			startActivity(intent);			
		}
		if (item.getItemId() == R.id.milestonepage)
		{			
			th.setCurrentTab(0);
		}
		if (item.getItemId() == R.id.quicklistpage)
		{			
			startActivity(new Intent(this, QuicklistTabHomePage.class));
		}

		if (item.getItemId() == R.id.changeprofilepage)
		{			
			intent = new Intent(TabHomePage.this, PickProfile.class);
			intent.putExtra("NoUsers", false);
			intent.putExtra("ModeSelected", "SelectProfile");
			startActivity(intent); 			
		}

		return super.onOptionsItemSelected(item);
	}





	//lets you change tabs within an activity
	public void switchTab(int tab){
		th.setCurrentTab(tab);
	}





}

